﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;


namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class CallDetailControl : RSBaseUserControl
    {
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
                    
            if(!Page.IsPostBack)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                int CompanyId = userIdentity.RSCompanyId;
                
                RSSBMCommonFunctions.ApplyAttributeRule(this, CompanyId, "CallDetailControl.ascx");
            }
        }

        public void SetControlEnabledState(bool bstate = false)
        {
            
            RSSBMCommonFunctions.SetControlsEnabledState(this, bstate);
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string prevurl = string.Empty;
            if (Session["PreviousURL"] != null)
                prevurl = Session["PreviousURL"].ToString();
            else
                prevurl = Request.UrlReferrer.ToString();

            Response.Redirect(prevurl);

        }
        protected void btnEditCall_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                btnEditCall.Visible = false;
                btnSaveCall.Visible = true;
                btnDeleteCall.Visible = true;
                SetControlEnabledState(true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void btnDeleteCall_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int CompanyId = userIdentity.RSCompanyId;
                int CallId = txtCallId.Text == "" ? 0 : Convert.ToInt32(txtCallId.Text);

                if (CompanyId > 0 && CallId > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@callid", SqlDbType.Int, 0, ParameterDirection.Input, CallId);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblCallLocation WHERE CallId=@callid AND CompanyId=@companyid; DELETE FROM rs_tblCall WHERE CallId=@callid AND CompanyId=@companyid;");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Service Call Successfully Deleted!", "RSViewCalls.aspx", true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void btnSaveCall_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int CompanyId = userIdentity.RSCompanyId;

                int CallId = txtCallId.Text == "" ? 0 : Convert.ToInt32(txtCallId.Text);
                string Subject = txtSubject.Text.Trim();
                string Description = txtCallDescription.Text.Trim();
                string Response = txtCallResponse.Text.Trim();
                string CallDT = txtDate.Text + " " + txtTime.Text.Trim();
                int CallTypeId = ddlCallType.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlCallType.SelectedValue);
                int StatusId = ddlCallStatus.SelectedValue == "0" ? 0 : Convert.ToInt32(ddlCallStatus.SelectedValue);

                SqlCommand sqlCmd = new SqlCommand();

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Subject", SqlDbType.NVarChar, 150, ParameterDirection.Input, Subject);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, -1, ParameterDirection.Input, Description);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Response", SqlDbType.NVarChar, -1, ParameterDirection.Input, Response);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CallDT", SqlDbType.NVarChar, 25, ParameterDirection.Input, CallDT);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@StatusId", SqlDbType.Int, 0, ParameterDirection.Input, StatusId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CallTypeId", SqlDbType.Int, 0, ParameterDirection.Input, CallTypeId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CallId", SqlDbType.Int, 0, ParameterDirection.Input, CallId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveCall");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                ucModalActionMessageControl.ShowModal("Service Call Successfully Saved!", "", true);
                btnEditCall.Visible = true;
                btnSaveCall.Visible = false;
                btnDeleteCall.Visible = false;
                SetControlEnabledState(false);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }

        private string GetRedirectURL()
        {
            int CallId = txtCallId.Text == "" ? 0 : Convert.ToInt32(txtCallId.Text);
            string url = "RSCallDetail.aspx?callid=" + CallId;
            return url;
        }
       
        public bool LoadCallDataByID(int companyid, int callid = 0)
        {
            bool bHasValue = false;
            if (companyid > 0)
            {
                RSSBMDataLookupLoader.LoadTypeDDL(companyid, "CALL_CAT", ddlCallType);
                RSSBMDataLookupLoader.LoadStatusDDL(companyid, "CALL_CAT", ddlCallStatus);

                if (callid > 0)
                {
                    SqlCommand myCommand;
                    SqlDataReader myDataReader;
                    SqlParameter param;
                    SqlConnection myConnection;
                    String sqlSELECT = "select * from rs_tblCall where CallId=@callid and companyid=@companyid";

                    myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                    myCommand = new SqlCommand(sqlSELECT, myConnection);

                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@callid";
                    param.Value = callid;
                    myCommand.Parameters.Add(param);

                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@companyid";
                    param.Value = companyid;
                    myCommand.Parameters.Add(param);

                    myConnection.Open();
                    myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    if (myDataReader.HasRows)
                    {
                        myDataReader.Read();
                        txtCallId.Text = callid.ToString();
                        txtSubject.Text = myDataReader["Subject"].ToString();
                        DateTime calldt = Convert.ToDateTime(myDataReader["CallDT"].ToString());
                        txtTime.Text = calldt.ToShortTimeString();
                        txtDate.Text = calldt.ToShortDateString();
                        if(myDataReader["CallTypeId"] != DBNull.Value)
                            ddlCallType.SelectedValue = myDataReader["CallTypeId"].ToString();
                        txtCallDescription.Text = myDataReader["Description"].ToString();
                        txtCallResponse.Text = myDataReader["Response"].ToString();
                        if(myDataReader["StatusId"] != DBNull.Value)
                            ddlCallStatus.SelectedValue = myDataReader["StatusId"].ToString();
    
                        bHasValue = true;
                        btnEditCall.Visible = true;
                        SetControlEnabledState(false);
                    }
                    myConnection.Close();

                }
            }
            return bHasValue;
        }

    }
}